package com.youfan.control;

import com.alibaba.fastjson.JSONObject;
import com.youfan.entity.YoufanProduct;
import com.youfan.re.Result;
import com.youfan.service.ProductService;
import com.youfan.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis")
public class RedisController {
    Logger logger= LoggerFactory.getLogger(RedisController.class);
    @Autowired
    RedisService redisService;

    @Autowired
    ProductService productService;

    /**
     * 设置Str缓存
     * @param key
     * @param val
     * @return
     */
    @RequestMapping(value = "setStr")
    public String setStr(String key, String val){
        try {
            redisService.setStr(key, val);
            return "success";
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 根据key查询Str缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "getStr")
    public String getStr(String key){
        return redisService.getStr(key);
    }


    /**
     * @param key
     * @return
     */
    @RequestMapping(value = "delStr")
    public String delStr(String key){
        try {
            redisService.del(key);
            return "success";
        } catch (Exception e){
            return "error";
        }
    }


    /**
     * 设置obj缓存
     * @return
     */
    @RequestMapping(value = "updateProduct")
    public String updateProduct(@RequestBody JSONObject jsonObject){
        try {
            String key = jsonObject.getString("key");
            YoufanProduct productInfo = jsonObject.getObject("productInfo", YoufanProduct.class);
            redisService.setObj(key, productInfo);
            return "success";
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }
    @RequestMapping(value = "getProduct")
    @ResponseBody
    public YoufanProduct getProduct(Long id){
        String productInfo=redisService.getStr(id+"");
        YoufanProduct product=null;
        if(StringUtils.isBlank(productInfo))
        {
            logger.info("=========>getProduct 数所库查询");
            YoufanProduct youfanProduct=new YoufanProduct();
            youfanProduct.setId(id);
            Result<YoufanProduct> result= productService.view(youfanProduct);
            product=result.getData();
            redisService.setStr(id+"",JSONObject.toJSONString(product));
        }else {
            logger.info("=========>getProduct 缓存查询");
            product=JSONObject.parseObject(productInfo,YoufanProduct.class);
        }

        return product;
    }
    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result<YoufanProduct> view(@RequestBody YoufanProduct youfanProductParam)
    {
        Long id= Long.valueOf(youfanProductParam.getId());
        String productInfo=redisService.getStr(id+"");
        YoufanProduct product=null;
        if(StringUtils.isBlank(productInfo))
        {
            logger.info("=========>view 数所库查询");
            YoufanProduct youfanProduct=new YoufanProduct();
            youfanProduct.setId(id);
            Result<YoufanProduct> result= productService.view(youfanProduct);
            product=result.getData();
            redisService.setStr(id+"",JSONObject.toJSONString(product));
        }else {
            logger.info("=========>view 缓存查询");
            product=JSONObject.parseObject(productInfo,YoufanProduct.class);
        }
        return Result.success(product,"查询单个对象成功");
    }

    @RequestMapping(method = RequestMethod.POST,value = "updateWithoutFile")
    @ResponseBody
    public Result<YoufanProduct> updateWithoutFile(@RequestParam(value = "formdata" ) String formdata)
    {
        try {

            YoufanProduct youfanProduct = JSONObject.parseObject(formdata, YoufanProduct.class);
            long id = youfanProduct.getId();
            Result<YoufanProduct> result = productService.updateWithoutFile(formdata);
            YoufanProduct youfanProduct1=result.getData();
            redisService.setStr(youfanProduct1.getId()+"",JSONObject.toJSONString(youfanProduct1));
            return Result.success(youfanProduct1,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "updateProduct")
    @ResponseBody
    public Result<YoufanProduct> updateProduct(@RequestBody YoufanProduct youfanProduct)
    {
        try {

            String json = JSONObject.toJSONString(youfanProduct);
            redisService.setStr(youfanProduct.getId()+"",json);
            return Result.success(youfanProduct,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }
//    /**
//     * 获取obj缓存
//     * @param key
//     * @return
//     */
//    @RequestMapping(value = "getObj")
//    public Object getObj(String key){
//        return redisService.getObj(key);
//    }


    /**
     * 删除obj缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "delObj")
    public Object delObj(String key){
        try {
            redisService.delObj(key);
            return "success";
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

}

